﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using ShakeGestures;

namespace Spinning.Spinning.Entity.Scenes
{
    class UserEarnLifeTimeScene : Scene
    {
        AnimationSprite spriteSpinning, spriteArrow, timerSprite, timerSprite10, cycleSprite, cycleSprite10;
        Sprite spriteArrow2;
        CircleGesture cg; 
        float pos_X;
        TextEntity te;
        bool isTimesUp = false;

        int cycleCount, endTimer;


        String stageName;
        
        public UserEarnLifeTimeScene(String stage) 
        {
            stageName = stage;
        }

        public override void OnLoad()
        {
            base.OnLoad();
            endTimer = 10;//倒數10秒
            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            cg = new CircleGesture();
            cg.Position = new Vector2(240, 550);
            cg.BigRadius = 260;
            cg.SmailRadius = 0;
            cg.ZIndex = Constants.debugText;
            //cg.OnLoad();
            TouchManager.RegisteTouchArea(cg);
            this.Attach(cg);
            cg.OnRunCircleEvent += AddCycleCount;
            cg.OnAreaChangeEvent += OnAreaChange;

            spriteSpinning = new AnimationSprite();
            spriteSpinning.textureName = "wind/big_spin";
            spriteSpinning.Position = new Vector2(240 - 157, 550 - 131);
            pos_X = spriteSpinning.Position.X;
            spriteSpinning.ZIndex = Constants.player;
            spriteSpinning.OnLoad();
            spriteSpinning.SetAnimation(new Point(1, 2), new int[] { 1 });
            spriteSpinning.Alpha = 1;
            this.Attach(spriteSpinning);

            spriteArrow = new AnimationSprite();
            spriteArrow.textureName = "wind/arrow";
            spriteArrow.Position = new Vector2(240 - 206, 390);
            spriteArrow.ZIndex = Constants.player;
            spriteArrow.OnLoad();
            spriteArrow.SetAnimation(new Point(2, 2), new int[] { 0 });
            spriteArrow.Alpha = 1;
            this.Attach(spriteArrow);

            spriteArrow2 = new Sprite();
            spriteArrow2.textureName = "wind/arrow_orange";
            spriteArrow2.Position = new Vector2(240 - 170, 240);
            spriteArrow2.ZIndex = Constants.player;
            spriteArrow2.OnLoad();
            spriteArrow2.Alpha = 0;
            this.Attach(spriteArrow2);

            timerSprite = new AnimationSprite();
            timerSprite.textureName = "number";
            timerSprite.ZIndex = Constants.player;
            timerSprite.OnLoad();
            timerSprite.SetAnimation(new Point(3, 4));
            timerSprite.Position = new Vector2(80, 45);
            this.Attach(timerSprite);

            timerSprite10 = new AnimationSprite();
            timerSprite10.textureName = "number";
            timerSprite10.ZIndex = Constants.player;
            timerSprite10.OnLoad();
            timerSprite10.SetAnimation(new Point(3, 4));
            timerSprite10.Position = new Vector2(20, 45);
            this.Attach(timerSprite10);

            Sprite timeSecond = new Sprite();
            timeSecond.textureName = "second";
            timeSecond.ZIndex = Constants.player;
            timeSecond.OnLoad();
            timeSecond.Position = new Vector2(145, 80);
            this.Attach(timeSecond);

            cycleSprite = new AnimationSprite();
            cycleSprite.textureName = "number";
            cycleSprite.ZIndex = Constants.player;
            cycleSprite.OnLoad();
            cycleSprite.SetAnimation(new Point(3, 4));
            cycleSprite.Position = new Vector2(380, 45);
            this.Attach(cycleSprite);

            cycleSprite10 = new AnimationSprite();
            cycleSprite10.textureName = "number";
            cycleSprite10.ZIndex = Constants.player;
            cycleSprite10.OnLoad();
            cycleSprite10.SetAnimation(new Point(3, 4));
            cycleSprite10.Position = new Vector2(320, 45);
            this.Attach(cycleSprite10);

            //te = new TextEntity();
            //ResourceManager.ContentRoot = "";
            //te.fontString = "TestFont";
            //te.OnLoad();
            //te.Origin = new Vector2(0.5f);
            //te.Position = new Vector2(200, 50);
            //te.Color = Color.Blue;
            ////te.Alpha = 0;
            //te.ZIndex = Constants.debugText;
            //Attach(te);

            ShakeGesturesHelper.Instance.ShakeGesture += Instance_ShakeGesture;
            ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
            ShakeGesturesHelper.Instance.Active = false;

            SortChildren();
        }

        public void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
        {
            ShakeGesturesHelper.Instance.Active = false;
            Game1.stageManager.LifeTimeEarnByUser = cycleCount;
            Game1.stageManager.GoToScene(stageName);
        }

        public void AddCycleCount() 
        {
            if (!isTimesUp)
                cycleCount++;
        }

        int _gameTime;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            _gameTime += delta;
            if (!isTimesUp)
            {
                if (_gameTime / 1000 >= 1)
                {
                    _gameTime = _gameTime % 1000;
                    endTimer--;
                    if (endTimer == 0)
                        OnTimesUp();
                }
            }
            SetTimerSprite(endTimer);
            SetCycleSprite(cycleCount);
            if (!cg.isPress) spriteArrow.SetChangeFrameAnimation(new int[] { 0 });
            //te.Value = "C Count:" + cycleCount;
        }

        void OnAreaChange()
        {
            spriteArrow.SetChangeFrameAnimation(new int[] { adjustInt(cg.CurrentArea) });
        }

        int adjustInt(int a)
        {
            switch (a)
            {
                case 0:
                    return 1;
                case 1:
                    return 3;
                case 2:
                    return 2;
            }
            return 0;
        }

        void SetTimerSprite(int i)
        {
            if (i < 0) i = 0;
            timerSprite10.SetChangeFrameAnimation(new int[] { i / 10 });
            timerSprite.SetChangeFrameAnimation(new int[] { i % 10 });
        }

        void SetCycleSprite(int i)
        {
            if (i < 0) i = 0;
            cycleSprite10.SetChangeFrameAnimation(new int[] { i / 10 });
            cycleSprite.SetChangeFrameAnimation(new int[] { i % 10 });
        }

        public void OnTimesUp() 
        {
            isTimesUp = true;
            spriteArrow.Alpha = 0;
            spriteArrow2.Alpha = 1;
            ShakeGesturesHelper.Instance.Active = true;
            //Game1.stageManager.LifeTimeEarnByUser = cycleCount;
            //Game1.stageManager.GoToScene(stageName);
        }

        public override Scene Clone()
        {
            Scene scene = new UserEarnLifeTimeScene(stageName);
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
